{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "39d3abd9",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# PyTorch介绍以及tensor类型 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73bb046b",
   "metadata": {},
   "source": [
    "PyTorch的安装：<br>\n",
    "https://pytorch.org/get-started/locally/"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f65f6546",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "库的导入："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6f334e1d",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "import torch "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e3d48f9",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "查看是否可以使用cuda："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7747b4af",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.cuda.is_available()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6fd2340",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "tensor 创建:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c932b440",
   "metadata": {},
   "source": [
    "类似numpy中ndarray<br>\n",
    "但是tensor可以在GPU上运算\n",
    "\n",
    "从numpy的ndarray来看tensor的几种创建方式："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0b25440b",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.6973, 0.2246, 0.1505, 0.8619],\n",
       "        [0.1192, 0.0958, 0.2751, 0.8599],\n",
       "        [0.1140, 0.7822, 0.2947, 0.9480]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "torch.ones(2, 3)\n",
    "torch.zeros(3, 3)\n",
    "torch.empty(3, 2)\n",
    "torch.rand(3, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5a78b3a",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "从已有数据构建:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cb354d6f",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.float32"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.tensor([4.5, 5, 4])\n",
    "x.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1edc6dc6",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "产生和原有数据形状相同的tensor:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ad0a962e",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.3667, -0.1994,  1.5210, -1.6244],\n",
       "        [ 0.9047,  0.7310, -1.0190,  0.5774],\n",
       "        [ 0.1183,  0.1233, -0.0443,  3.3230]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.rand(3, 4)\n",
    "x = torch.randn_like(a, dtype=torch.float)\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7bc786d0",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "得到tensor的形状:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9bca5e20",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 4])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e45684a3",
   "metadata": {
    "scrolled": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 4])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.size()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8e20f2d",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Resizing: 如果你希望resize/reshape一个tensor，<br>可以使用view："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d4968e33",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.3667, -0.1994,  1.5210],\n",
       "        [-1.6244,  0.9047,  0.7310],\n",
       "        [-1.0190,  0.5774,  0.1183],\n",
       "        [ 0.1233, -0.0443,  3.3230]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = x.view(-1, 3)\n",
    "z"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18f3559a",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "使用transpose，从轴的设置上进行转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "798253d1",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.3667, -1.6244, -1.0190,  0.1233],\n",
       "        [-0.1994,  0.9047,  0.5774, -0.0443],\n",
       "        [ 1.5210,  0.7310,  0.1183,  3.3230]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z.transpose(1, 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c10a5c2d",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "使用`item`方法把tensor的value变成python的数值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "40edff53",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "i = torch.rand(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "83bf3b6a",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.2125])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "09cd2dc3",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.21249717473983765"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i.item()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9caef131",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "`ndarray`和`tensor`互转换操作："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c5c4c2f0",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 1. 1. 1. 1.]\n",
      "tensor([1., 1., 1., 1., 1.], dtype=torch.float64)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.ones(5) \n",
    "b = torch.from_numpy(a) \n",
    "print(a) \n",
    "print(b)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "bc92c314",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([1., 1., 1., 1., 1.])\n",
      "[1. 1. 1. 1. 1.]\n"
     ]
    }
   ],
   "source": [
    "a = torch.ones(5) \n",
    "print(a)\n",
    "b = a.numpy()\n",
    "print(b) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "447ce7cf",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#### 总结：\n",
    "1. 可以类比`ndarray`中创建方式对`tensor`进行创建。\n",
    "2. 得到形状可以通过`shape`属性和`size()`方法。\n",
    "3. 改变形状的方法有`view()`和`transpose()`等。\n",
    "3. 得到数值可以通过`data`属性和`item()`方法。\n",
    "4. `ndarray`和`tensor`互操作方法`torch.from_numpy()`和`tensor.numpy()`"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
